<?php
namespace app\api\controller\deerhome\shop;

use app\common\controller\Api;
use think\Db;
/**
 * 基础类
*/
class Base extends Api
{
	protected $noNeedLogin = ['*']; 
    protected $_user;
    protected $_token;
    protected $_openid;
    protected $_deviceType;
    protected $_version;
    protected $_allowedDeviceTypes=["wxapp","ios","android","h5"];

	public function __construct() {
        parent::__construct();
        $this->_token = $this->request->header('Deer-Api-Token');
        $this->_openid = $this->request->header('Deer-Api-Openid','');
        $this->_version = $this->request->header('Deer-Api-Version');
        $this->_deviceType = strtolower($this->request->header('Deer-Api-Type'));

        //第三方通知不校验
        if(stripos($this->request->controller(),"notify")!==false){
            return;
        }

        $this->_initUser();
    }
	private function _initUser(){
        if (empty($this->_token)) {
            return ;
        }
        $user=Db::name("deerhome_user")
        ->where("token",$this->_token)->find();
        if($user){
            $this->_user = $user;
        }
    }

	public function ok($data = [],$msg = 'success'){
        $arr=[];
        $arr['code']=0;
        $arr['msg']=$msg;
        $arr['data']=$data;
		echo json_encode($arr);
		exit;
	}
	public function err($msg = 'error',$alert=0,$data = [],$code = 1){
        $arr=[];
        $arr['code']=$code;
        $arr['msg']=$msg;
        $arr['data']=$data;
        $arr['alert']=$alert;
		echo json_encode($arr);
		exit;
	}
    protected function fixImg($img,$face=false){
        if($img==""){
            return '';
        }
        $cdnurl=\think\Config::get('upload.cdnurl');
        if(!$cdnurl){
            if(substr(strtolower($img),0,4)!="http"){
                return $this->request->domain().$img;
            }
            return $img;
        }
        if(strpos($img,$cdnurl)===0 && $face){
            return $img."!item";
        }
        if(strpos($img,"http")===0){
            return $img;
        }
        if(substr(strtolower($img),0,4)!="http" && $face){
            return $cdnurl.$img."!item";
        }
        return cdnurl($img, true);
    }
    //返回周几
    protected function getWeek($date,$dw="周"){
        $weekarray=array("日","一","二","三","四","五","六");
        return $dw.$weekarray[date("w",strtotime($date))];
    }
}
